;
(function (window, angular) {
	'use strict';
	angular.module("Controller.Stock.StorageRecords", [])
		.controller("StorageRecordsCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "UserContextService", "Upload", "$timeout", "EventBus", "$filter", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, UserContextService, Upload, $timeout, EventBus, $filter) {
			$scope.datePickerFlag = [false, false];
			$scope.markInfo = {
				allChecked: false,
				curRow: 0
			};
			$scope.pageInfo = {
				totalPages: 1,
				totalRows: 0,
				pageSize: 10,
				curPage: 1
			};
			$scope.dataList = [];
			$scope.condition = {
				type: 0
			};
			$scope.toolbarConfig = {
				showAdvanceSearch: false,
				showColConfig: false,
				showMateriel: false
			};
			$scope.exportParam = {
				sectionUrl: ybHttp.RequestUrl("device", "/warehouseInventory/exportForInOutRecord"),
				sectionParam: { ids: [] },
				allUrl: ybHttp.RequestUrl("device", "/warehouseInventory/exportAllForInOutRecord")
			};
			$scope.tableConfig = {
				fixedWidth: 0,
				scrollWidth: 1940,
				tableCol: [
					{
						type: "",
						name: "业务单号",
						dataField: "businessOrder",
						width: 180,
						isShow: true,
						isFixed: false,
						toSort: true,
						sortType: ""
					},
					{
						type: "",
						name: "条形码",
						dataField: "barCode",
						width: 150,
						isShow: true,
						isFixed: false,
						toSort: true,
						sortType: ""
					},
					{
						type: "",
						name: "拆箱前条码",
						dataField: "sourceBarcode",
						width: 150,
						isShow: true,
						isFixed: false,
						toSort: true,
						sortType: ""
					},
					{
						type: "",
						name: "零件编号",
						dataField: "materialNo",
						width: 140,
						isShow: true,
						isFixed: false,
						toSort: true,
						sortType: ""
					},
					{
						type: "",
						name: "零件中文名",
						dataField: "materialName",
						width: 150,
						isShow: true,
						isFixed: false,
						toSort: true,
						sortType: ""
					},
					{
						type: "",
						name: "零件英文描述",
						dataField: "materialNameEn",
						width: 150,
						isShow: true,
						isFixed: false,
						toSort: true,
						sortType: ""
					},
					{
						type: "",
						name: "仓库编码",
						dataField: "warehouseCode",
						width: 100,
						isShow: true,
						isFixed: false,
						toSort: true,
						sortType: ""
					},
					{
						type: "",
						name: "仓库描述",
						dataField: "warehouseDesc",
						width: 100,
						isShow: true,
						isFixed: false,
						toSort: true,
						sortType: ""
					},
					{
						type: "",
						name: "库区",
						dataField: "areaCode",
						width: 100,
						isShow: true,
						isFixed: false,
						toSort: true,
						sortType: ""
					},
					{
						type: "",
						name: "储位",
						dataField: "locationCode",
						width: 100,
						isShow: true,
						isFixed: false,
						toSort: true,
						sortType: ""
					},
					{
						type: "",
						name: "操作数量",
						dataField: "amount",
						width: 100,
						isShow: true,
						isFixed: false,
						toSort: true,
						sortType: ""
					},
					{
						type: "",
						name: "零件原数量",
						dataField: "totalAmount",
						width: 100,
						isShow: true,
						isFixed: false,
						toSort: true,
						sortType: ""
					},
					{
						type: "",
						name: "计量单位",
						dataField: "unit",
						width: 100,
						isShow: true,
						isFixed: false,
						toSort: true,
						sortType: ""
					},
					{
						type: "",
						name: "操作时间",
						dataField: "createDateShow",
						width: 100,
						isShow: true,
						isFixed: false,
						toSort: true,
						sortType: ""
					},
					{
						type: "",
						name: "创建人",
						dataField: "createrName",
						width: 80,
						isShow: !$scope.isEN,
						isFixed: false,
						toSort: true,
						sortType: ""
					},
					{
						type: "",
						name: "是否冻结",
						dataField: "frozernStatusShow",
						width: 100,
						isShow: !$scope.isEN,
						isFixed: false,
						toSort: true,
						sortType: ""
					},
					{
						type: "",
						name: "出入库类型",
						dataField: "businessSingle",
						width: 100,
						isShow: true,
						isFixed: false,
						toSort: true,
						sortType: ""
					},
					{
						type: "",
						name: "出入库描述",
						dataField: "businessSingleDes",
						width: 100,
						isShow: !$scope.isEN,
						isFixed: false,
						toSort: true,
						sortType: ""
					},
					{
						name: "出入库方向",
						dataField: "typeShow",
						width: 100,
						isShow: $scope.isWJ,
						isFixed: false,
					},
					{
						name: "说明",
						dataField: "remark",
						width: 200,
						isShow: $scope.isWJ,
						isFixed: false,
					},
					{
						name: "DSC单据号",
						dataField: "dscDeliveryOrderNo",
						width: 200,
						isShow: $scope.isWJ,
						isFixed: false,
					},
					{
						type: "operate",
						name: "操作",
						width: 60,
						isShow: true,
						isFixed: true,
				}
				]
			};

			var btnPermissionCode = {
				storageRecordsExport: window.ActionCode.storageRecordsExport,
				storageRecordsChehui: window.ActionCode.storageRecordsChehui,
				storageRecordsChehuiBatch: window.ActionCode.storageRecordsChehuiBatch
			};

			$scope.btnPermission = {
				storageRecordsExport: true,
				storageRecordsChehui: true,
				storageRecordsChehuiBatch: true
			};

			function setBtnPermission() {
				for (var key in btnPermissionCode) {
					$scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
				}
			}

			$scope.setFixedWidth = function () {
				$scope.tbStyle = {
					billFixedWidth: { left: $scope.tableConfig.fixedWidth + "px" }
				};
			};

			function getDataList(curPage) {
				$scope.exportParam.sectionParam.ids = [];
				var service = "device";
				var reqUrl = "/warehouseInventory/listForInOutRecord";
				var param = {};
				param.pageSize = $scope.pageInfo.pageSize;
				if (!!curPage) {
					$scope.pageInfo.curPage = curPage;
				}
				param.pageNum = $scope.pageInfo.curPage;
				param.filters = $scope.condition;
				ybHttp.Post(service, reqUrl, param).then(function (resp) {
					if (resp.code !== 0) {
						ybWidget.ShowMessage(resp, MESG_TYPE.Error);
						return;
					}
					angular.forEach(resp.value.list, function (item) {
						item.isChecked = false;
						item.createDateShow = $filter('YbDate')(item.createDate, 'yyyy-MM-dd HH:mm:ss');
						item.businessSingleDes = $filter('orderType')(item.businessSingle);
						item.frozernStatusShow = $filter('TrueOrFalse')(item.frozernStatus, 'normal');
						item.typeShow = $filter("ProgressStatus")(item.type, "inOutType");
					});
					$scope.pageInfo.totalRows = resp.value.total;
					$scope.pageInfo.totalPages = resp.value.pages;
					$scope.dataList = resp.value.list;
				});
			}

			$scope.switchDataPicker = function (index) {
				$scope.datePickerFlag = [false, false];
				if (angular.isDefined(index)) {
					$scope.datePickerFlag[index] = true;
				}
			};

			$scope.switchToolbar = function (toolbar) {
				if (!!$scope.toolbarConfig["show" + toolbar]) {
					$scope.toolbarConfig["show" + toolbar] = false;
				} else {
					$timeout(function () {
						$scope.toolbarConfig["show" + toolbar] = true;
					});
				}
			};

			$scope.checkDate = function (obj, startFiled, endFiled) {
				var timeFormatObj = ybWidget.CheckDate(obj[startFiled], obj[endFiled]);
				if (!!timeFormatObj.tStart) {
					obj[startFiled] = timeFormatObj.tStart;
				}
				if (!!timeFormatObj.tEnd) {
					obj[endFiled] = timeFormatObj.tEnd;
				}
			};

			$scope.selectRow = function (index) {
				$scope.markInfo.cueRow = index
			};

			$scope.search = function (curpage) {
				localStorage.setItem($scope.storageKey, JSON.stringify($scope.condition));
				getDataList(curpage);
			};

			$scope.reset = function () {
				$scope.condition = {
					type: 0
				};
			};

			$scope.actionUrl = ybHttp.RequestUrl("device", "/warehouseInventory/download");

			function geBillIds() {
				$scope.exportParam.sectionParam.ids = [];
				angular.forEach($scope.dataList, function (item) {
					if (!!item.isChecked) {
						$scope.exportParam.sectionParam.ids.push(item.id);
					}
				});
			}
			function checkAll() {
				var selectAll = false;
				for (var i = 0, len = $scope.dataList.length; i < len; i++) {
					if (!$scope.dataList[i].isChecked) {
						selectAll = false;
						break;
					}
					selectAll = true;
				}
				return selectAll;
			}

			$scope.selectSingle = function (item) {
				if (!item.isChecked) {
					$scope.markInfo.allChecked = false;
				} else {
					if (!checkAll()) {
						$scope.markInfo.allChecked = false;
					} else {
						$scope.markInfo.allChecked = true;
					}
				}
				geBillIds();
			};

			$scope.selectAll = function () {
				for (var i = 0, len = $scope.dataList.length; i < len; i++) {
					$scope.dataList[i].isChecked = $scope.markInfo.allChecked;
				}
				geBillIds();
			};


			(function () {
				$scope.storageKey = $state.current.name;
				UserContextService.LocalCache($scope.storageKey, $scope.condition);

				$scope.setFixedWidth();
				UserContextService.SetBtnPermission(setBtnPermission);
			})();
		}])
		;
})(window, window.angular);